/**
 * 顺序栈
 * @param <T>
 */
public class sequenceStack<T> {
    final int MaxSize = 10;
    private T[] stackArray;
    private int top;
    /*栈的初始化*/
    public sequenceStack(){
        top=-1;
        stackArray=(T[])new Object[MaxSize];
    }
    public sequenceStack(int n){
        if (n<=0){
            System.out.println("数组长度要大于0，否则退出程序运行！");
            System.exit(1);
        }
        top=-1;
        stackArray=(T[])new Object[n];
    }

    /*入栈*/
    public void push(T obj){
        if (top == stackArray.length-1){
            T[] p = (T[]) new Object[top*2+2];
            for (int i = 0; i <= top; i++){
                p[i] = stackArray[i];
            }
            stackArray = p;
        }
        top++;
        stackArray[top] = obj;
    }
    /*出栈*/
    public T pop(){
        if (top==-1){
            System.out.println("数据栈为空，无法删除元素");
            return null;
        }
        top--;
        return stackArray[top+1];
    }
//    取栈顶元素
    public T getHead(){
        if (top==-1){
            System.out.println("数据栈为空，无法删除元素");
            return null;
        }
        return stackArray[top];
    }
//    判空
    public boolean isEmpty(){
        return top==-1;
    }
//    栈的长度
    private int size(){
        return top+1;
    }
//    销毁栈
    public void clear(){
        top = -1;
    }
}
